<?php

namespace App\Admin\Controllers\Admin;


use App\Models\Admin\AdminNotification;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class NotificationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $user = Admin::user();
        $model = AdminNotification::where("admin_id",$user->id)->orderBy("id","desc");
        $this->addScript();
        return Grid::make($model, function (Grid $grid) {
            $grid->column('id')->width(100)->sortable()->setAttributes(["class"=>"grid-id"]);
            $grid->column('level',"消息级别")->width(100)->display(function ($value){
                if ($value == 1 ) return '<i class="fa fa-circle text-green"></i>';
                if ($value == 2 ) return '<i class="fa fa-circle text-orange"></i>';
                if ($value == 3 ) return '<i class="fa fa-circle text-red"></i>';
                return '<i class="fa fa-circle text-primary"></i>';
            });
            $grid->column('title')->setAttributes(["class"=>"grid-title"])->width(500)->modal(function (Grid\Displayers\Modal $modal) use($grid){
                $modal->title('通知详情');
                return "<p style='min-height: 400px;padding: 20px'>{$this->content}</p>";
            });
            #$grid->column('content');
            $grid->column('read_at')->setAttributes(["class"=>"grid-read"])->width(200)->display(function (){
                return $this->read_at ? "<i class='fa feather icon-check text-green'></i> 已读" :"<i class='fa feather icon-x text-red'></i> 未读";
            });
            $grid->filter(function (Grid\Filter $filter){
                $filter->panel()->expand();
                $filter->equal("state")->select(["1" =>'已读','0' => '未读'])->width(2);
            });
            $grid->column('created_at')->width(200);
            $grid->column('updated_at')->width(200);
            $grid->disableActions();
            $grid->disableCreateButton();
        });
    }

    private function addScript()
    {
        $js = <<<JS
$("#grid-table tbody").on("click",".grid-title a",function (){
    var id = $(this).parents("tr").find('.grid-id').text();
    var change = $(this).parents("tr").find('.grid-read');
    $.ajax({
        url: "notification/"+id,
        success: function (){
            if ($.trim(change.text()) == "未读"){
                 change.html('<i class="fa feather icon-check text-green"></i> 已读')
                var cnt = $(".notification-count").text();
                console.log(change.text())
                cnt = parseInt(cnt)
                cnt--
                $(".notification-count").text(cnt)
            }
        }
    });
});
JS;
        Admin::script($js);
    }

    public function read($id)
    {
        $notify = AdminNotification::find($id);
        if($notify && $notify->admin_id == Admin::user()->id){
            $notify->read_at == null && $notify->update(["read_at"=>Carbon::now(),'state'=>1]);
        }
    }
}
